package com.gp.controller.goods;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gp.common.result.JsonResult;
import com.gp.controller.BaseController;
import com.gp.model.dto.ProductLevelInfo;
import com.gp.model.entity.BaseProductlevel;
import com.gp.service.IBaseProductlevelService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 弓成龙
 * @since 2017-11-26
 */
@RestController
@RequestMapping("/productType")
public class BaseProductlevelController extends BaseController {
  @Autowired
  private IBaseProductlevelService productService;

  /**
   * 获取分类信息，带子类List
   * @param orderId
   * @return
   */
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  public JsonResult list() {
    List<ProductLevelInfo> list = productService.list();
    return new JsonResult("获取成功", list);
  }

  /**
   * 添加一个分类信息
   * @param goods 需要增加的商品信息
   * @param bindingResult
   * @return
   */
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public JsonResult add(BaseProductlevel level) {
    boolean flag = productService.insertProductlevel(level);
    return flag ? renderSuccess("添加成功！") : renderError("分类添加失败！");
  }
  
  /**
   * 更新商品信息
   * @param entity
   * @return
   */
  @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
  public JsonResult update(BaseProductlevel entity) {
    return productService.updateById(entity) ? renderSuccess("更新成功！") :renderError("更新失败！");
  }
  
  /**
   * 删除一个商品
   * @param id 需要删除的商品的id
   * @return
   */
  @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
  public JsonResult del(@PathVariable String id) {
    boolean flag = productService.deleteById(Long.parseLong(id));
    return flag ? renderSuccess("删除成功！") : renderError("该分类删除失败！");
  }
  
  /**
   * 增加搜索条件的列表
   * @param orderId
   * @return
   */
  @RequestMapping(value = "/listByCondition", method = RequestMethod.GET)
  public JsonResult list(Page page,
      @RequestParam(value = "isSearch", required = false) Integer isSearch) {
    Page<BaseProductlevel> list = null;
    if (isSearch!=null && isSearch == 1) {
      Wrapper<BaseProductlevel> wrapper = new EntityWrapper<BaseProductlevel>();
      Map<String, Object> map = page.getCondition();
      wrapper.like("classname", (String) map.get("classname"));
      page.setCondition(null);
      list = productService.selectPage(page, wrapper);
      list.setCondition(map);
      return new JsonResult("获取成功", list);
    } else {
      return new JsonResult("获取成功", productService.list());
    }
  }

  /**
   * 
   * @param  
   * @return
   */
  @RequestMapping(value = "/listAll", method = RequestMethod.GET)
  public JsonResult listAll(Page page) {
    page = productService.selectPage(page);
    return new JsonResult("获取成功", page);
  }
}
